<?php              
				include "includes".DIRECTORY_SEPARATOR."start_session.php";
				
				
				define('DEFAULT_TABLE_ROWS', 3);
				
				
				#=================================================================================================================
				#=================================================================================================================
				#	Define all possible options in drop-down menu
				#=================================================================================================================
				#=================================================================================================================
			
				$form_for_table = array(
				
					
					#----------------------------------------------------------------------
					#						Tabel										  |
					#----------------------------------------------------------------------
					
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Denumire Material',
						  'post_name' 			 => 'denumire_material',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35),
				
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Cantitate',
						  'post_name' 			 => 'cantitate',
						  'regex'				 => '/^\d+(\.\d+)?$/',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35),
				
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Unitate Masura',
						  'post_name' 			 => 'unitate_masura',
						  'field_size' 			=> 40,	
						  'max_length' 			=> 35),
						  
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Data livrare',
						  'post_name' 			 => 'data',
						  'regex'				 => '/^\d+(\.\d+)?$/',
						  'field_size' 			=> 40,	
						  'max_length' 			=> 35),
				
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Observatii',
						  'post_name' 			 => 'observatii',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35,
						  'extra_space_after'	=> "yes" )

				);
			
			
				$form_fields_with_speficications = array();
			
				for($index = 1; $index <= DEFAULT_TABLE_ROWS; $index+=1) 
				{
					foreach( $form_for_table as $form_column_table )
					{
						if( isset( $form_column_table['post_name'] ) )
						{
							if( $form_column_table['post_name'] == "cod_produs" )
							{
								$form_column_table['header'] = "Produs ".strval($index)." :";
							}
							$form_column_table['post_name'] = "tabel_".strval($index)."_".$form_column_table['post_name'];
						}
						array_push( $form_fields_with_speficications,$form_column_table);
					}
				}
			
				array_push( $form_fields_with_speficications,
				
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Proiect',
						  'post_name' 			 => 'nume_proiect',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35,
						  'errors' 				=> array('empty_field_or_no_selection_error' => 'Nu a fost introdus numele proiectului<br />',
										    			   'no_regex_pattern_found_error' => 'Nume proiect invalid<br />' )
						  ),
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Data',
						  'post_name' 			 => 'data',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35,
						  'errors' 				=> array('empty_field_or_no_selection_error' => 'Nu a fost introdusa data<br />',
										    			   'no_regex_pattern_found_error' => 'Data introdusa este invalida<br />' )
						  ),
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'De la',
						  'post_name' 			 => 'de_la',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35,
						  'errors' 				=> array('empty_field_or_no_selection_error' => 'Nu a fost introdus nimic in campul \'de la\'<br />',
										    			   'no_regex_pattern_found_error' => 'Valoarea introdusa in campul \'de la\' este invalida<br />' )
						  ),
					array('field_type' 			=> 'text',
						  'field_display_name' 	=> 'Subiect',
						  'post_name' 			 => 'subiect',
						  'field_size' 			=> 40,
						  'max_length' 			=> 35,
						  'errors' 				=> array('empty_field_or_no_selection_error' => 'Nu a fost introdus nimic in campul \'subiect\'<br />',
										    			   'no_regex_pattern_found_error' => 'Valoarea introdusa in campul \'subiect\' este invalida<br />' )
						  )
				);


	
				
				#=================================================================================================================
				#	End of drop-down elemnts
				#=================================================================================================================
				
				# Generate form content based on provided data
				function generate_form_content($array_data)
				{
					if( !isset($array_data))
					{
						return "";
					}
						
					$get_form_content = "";
					$bFirst = 1;
													
					foreach($array_data as $form_field)
					{
						$change_row = 1;
						
						if(isset($form_field['same_row']))
							if ($form_field['same_row'] == "yes")
									$change_row = 0;
						
						if ( $bFirst )
						{
							$get_form_content .= "<tr>";
						}
						else
						{
							if( $change_row )
							{
								$get_form_content .= "</tr>";	
								$get_form_content .= "<tr>";
							}	
						}			
						
						$bFirst = 0;			
						
						if( isset($form_field['header']))
						{
							$get_form_content .= "</tr><tr><td>".$form_field['header']."</td></tr><tr>";
						}
							
						if( ! isset( $form_field['only_form_element'] ) )
							$get_form_content .= "<td><b><font color=\"".$form_field['current_color']."\">" . $form_field['field_display_name'] . "</font></b></td>";
						
						if( $form_field['field_type'] == 'text' )
						{
							$get_form_content .= "<td><input type =\"text\" name =\"" . $form_field['post_name'] . "\"";
							$get_form_content .= " value=\"" . $form_field['current_value'] . "\"";
							
	
							$get_form_content .= " size=\"" . $form_field['field_size'] . "\"";
							$get_form_content .= " maxlength=\"" . $form_field['max_length'] . "\"";
							$get_form_content .= " ></td>";
						}
						else if( $form_field['field_type'] == 'drop_down_list' )
						{
							
							if( isset($form_field['initial_values']) )
								$drop_down_list_options = drop_down_list_generator($form_field['initial_values'],$form_field['current_value']);	
							else
								$drop_down_list_options = drop_down_list_generator(null);	
							$get_form_content .= "<td><select name=\"".$form_field['post_name']."\">";
							$get_form_content .= $drop_down_list_options;
							$get_form_content .= "</select></td>";						
						}
						
						
						if( isset( $form_field['extra_space_after'] ))
						{
							$get_form_content .= "</tr>";	
							$get_form_content .= "<tr>";	
							$get_form_content .="<td></td>";
							$get_form_content .= "</tr>";	
							$get_form_content .= "<tr>";	
						}
						
								
					}	
					
					$get_form_content .="</tr>";
					return $get_form_content;
				}


				# Function used to generate drop down list menu
				function drop_down_list_generator($drop_down_list_options_array, $last_selected_value = null)
				{ 
					$drop_down_list_options = "";				   
				   	
					
					if( null == $drop_down_list_options_array )
							return $drop_down_list_options;	
					
					# Default value used when drop-down list is displayed	
					$drop_down_list_options = $drop_down_list_options."<option value=\"neselectat\">...</option>";
									
					# foreach element in dropdown-list render options
					# if last_selected_value is different than null than select that value as being the default one displayed in interface 
					
					
					foreach($drop_down_list_options_array as $drop_down_list_element_value)
				   	{
						
						if ( $last_selected_value != null )
						{
							if( $last_selected_value == $drop_down_list_element_value )
							{
								# if one of the options in drop-down list matches the provided value for last_selected_value make it the default one
								$drop_down_list_options = $drop_down_list_options."<option selected=\"selected\" value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
							}
							else
							{
								$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
							}
							
						}
						else
						{
							$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";
						}
				   	}
					return $drop_down_list_options;
				}

				
				if ( !defined('USERNAME') || !defined('GROUP') || GROUP != "admin" )
				{
					header("Location: index.php");
					exit;
				}			

				foreach($form_fields_with_speficications as $form_key => $form_value)
				{
					if( ! isset($form_fields_with_speficications[$form_key]['colors']) )
							$form_fields_with_speficications[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

					if( ! isset($form_fields_with_speficications[$form_key]['field_size']) )
							$form_fields_with_speficications[$form_key]['field_size'] = null;
					if( ! isset($form_fields_with_speficications[$form_key]['max_length']) )
							$form_fields_with_speficications[$form_key]['max_length'] = null;
					if( ! isset($form_fields_with_speficications[$form_key]['regex']) )
							$form_fields_with_speficications[$form_key]['colors'] = null;						  
					if( ! isset($form_fields_with_speficications[$form_key]['can_be_null']) )
							$form_fields_with_speficications[$form_key]['can_be_null'] =  false;
					
					if( $form_fields_with_speficications[$form_key]['field_type'] == 'text')
					{
						if( isset($form_fields_with_speficications[$form_key]['initial_values']) )
							$form_fields_with_speficications[$form_key]['current_value'] = $form_fields_with_speficications[$form_key]['initial_values'];
						else
							$form_fields_with_speficications[$form_key]['current_value'] =  '';
					}
					else if( $form_fields_with_speficications[$form_key]['field_type'] == 'drop_down_list')
					{
						$form_fields_with_speficications[$form_key]['current_value'] = "neselectat";
					}
					
					$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['first'];	
				}
				
				$form_content = generate_form_content($form_fields_with_speficications);
				$erori = "";
					
  				  $get_form=<<<GATA
	                   <form method="post" action = "necesar_materiale_add.php">
	
	                   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
					   $form_content
<tr><td></td><td><input type ="submit" value = "Trimite Necesar" ></td></tr>
					   
					   </table>
	                   </form>
GATA;


			   if($_SERVER['REQUEST_METHOD'] == 'GET')
			   {
				   	echo $get_form;
			   }
  
  
  				  if($_SERVER['REQUEST_METHOD'] == 'POST')
				  
				  {
				  			$ok = 1;

							foreach($form_fields_with_speficications as $form_key => $form_value)
							{
								if( isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
								{									
									if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
									|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
									{
										# Something went wrong with this form element so an error will be displayed to user
										$ok = 0;
										
										# Change color for element ; default will be red
										$form_fields_with_speficications[$form_key]['current_color'] = "red";
										if( isset($form_fields_with_speficications[$form_key]['colors'] ))
										{
											if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
											{
												$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['second'];		
											}
										}
										
										# Generate an error if nothing it's provided
										$default_generated_error = "";
										
										if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
										{
											$default_generated_error = "Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
										}
										else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
										{
											$default_generated_error = "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
										}
										else
										{
											$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
										}
										
										
										# Add the provided error or the default one
										if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
										{
											if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
											{
												$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
											}
											else
											{
												$erori = $erori.$default_generated_error;
											}
										}
										else
										{
											$erori = $erori.$default_generated_error;
										}
										
										continue;
									}
									
									# Check regex here
									if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
									{
										if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
										{
											
											if( preg_match($form_fields_with_speficications[$form_key]['regex'],$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
											{																					
												# Something went wrong with this form element so an error will be displayed to user
												$ok = 0;
												
												# Change color for element ; default will be red
												$form_fields_with_speficications[$form_key]['current_color'] = "red";
												if( isset($form_fields_with_speficications[$form_key]['colors'] ))
												{
													if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
													{
														$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['second'];		
													}
												}
												
												# Generate an error if nothing it's provided
												$default_generated_error = "";
												
												if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
												{
													$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['post_name']." sunt invalide</br>";
												}
												else
												{
													$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este invalid</br>";
												}
												
												
												# Add the provided error or the default one
												if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
												{
													if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
													{
														$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
													}
													else
													{
														$erori = $erori.$default_generated_error;
													}
												}
												else
												{
													$erori = $erori.$default_generated_error;
												}
											
												continue;
											}
											
			
										}
										
									}
									
									$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
								}
							}							
							
							if($ok != 1)
							{
																
  				$form_content = generate_form_content($form_fields_with_speficications);
  				$form_post=<<<GATA
	                   <form method="post" action = "necesar_materiale_add.php">
	
	                   <table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
					   $form_content
						<input type="hidden" name="number_of_rows" value="3"> 
<tr><td><input type ="submit" name="previzualizare" value = "Previzualizare" onclick="this.form.target='_blank';return true;">
</td><td><input type ="submit" value = "Trimite Necesar" onclick="this.form.target='_self';return true;" name="necesar"></td></tr>



					   </table>
	                   </form>
GATA;
					 			echo $form_post;
								echo "<font color=\"red\">".$erori."</font>";
							}
							else  
							{									
									echo "<br />";	
									echo "<center><h2><font color=\"#00CC00\">Necesar Materiale trimis cu succes ! </font></h2></center>";	
									
									echo "<br />";	
									echo $form_get;
							}
							
					}
						
?>


